import cv2
import numpy as np

# 读取图像路径
# image_path = '../Fig0457(a)(thumb_print).tif' 
image_path = '../Fig0335(a)(ckt_board_saltpep_prob_pt05).tif' 

# 读取图像并转换为灰度图像
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

# 检查图像是否加载成功
if image is None:
    print(f"Error: Unable to load image from {image_path}")
    exit()

cv2.imshow('0. Original Image', image)  # 显示原图

# 1. 均值平滑（Mean Blurring）
# 定义均值滤波的窗口大小
mean_kernel_size = (5, 5)  # 5x5 窗口
# 应用均值滤波
mean_blurred_image = cv2.blur(image, mean_kernel_size)
cv2.imshow("1. Mean Blurring", mean_blurred_image)

# 2. 高斯平滑（Gaussian Blurring）
# 定义高斯滤波的窗口大小和标准差
gaussian_kernel_size = (5, 5)  # 5x5 窗口
gaussian_sigma = 0  # 高斯核的标准差
# 应用高斯滤波
gaussian_blurred_image = cv2.GaussianBlur(image, gaussian_kernel_size, gaussian_sigma)
cv2.imshow("2. Gaussian Blurring", gaussian_blurred_image)

# 3. 中值滤波（Median Filtering）
# 定义中值滤波的窗口大小
median_kernel_size = 5  # 使用 5x5 窗口
# 应用中值滤波
median_blurred_image = cv2.medianBlur(image, median_kernel_size)
cv2.imshow("3. Median Blurring", median_blurred_image)

# 4. 双边滤波（Bilateral Filtering）
# 定义双边滤波的参数
bilateral_diameter = 9  # 邻域直径
bilateral_sigma_color = 75  # 色彩空间的标准差
bilateral_sigma_space = 75  # 坐标空间的标准差
# 应用双边滤波
bilateral_filtered_image = cv2.bilateralFilter(image, bilateral_diameter, bilateral_sigma_color, bilateral_sigma_space)
cv2.imshow("4. Bilateral Filtering", bilateral_filtered_image)

# 5. 方框滤波（Box Filtering）
# 定义方框滤波的窗口大小
box_kernel_size = (5, 5)  # 5x5 窗口
# 应用方框滤波
box_filtered_image = cv2.boxFilter(image, -1, box_kernel_size)
cv2.imshow("5. Box Filtering", box_filtered_image)

# 6. 扩展卷积（Convolution）
# 定义自定义卷积核
convolution_kernel = np.array([ [0, 1, 0],
                                [1, 1, 1], 
                                [0, 1, 0] ], dtype=np.float32)  # 3x3 卷积核
# 应用卷积
convolved_image = cv2.filter2D(image, -1, convolution_kernel)
cv2.imshow("6. Convolution", convolved_image)

# 7. 图像金字塔（Image Pyramid）
# 应用图像下采样（图像金字塔中的 pyrDown）
downsampled_image = cv2.pyrDown(image)
cv2.imshow("7. Pyramid Downsampling", downsampled_image)

# 等待按键事件并关闭所有显示窗口
cv2.waitKey(0)
cv2.destroyAllWindows()
